import { validateAtLeastOneField } from '@/utils/validator'
import { Empty, FormInstance } from 'antd'
export const searchConfig = ({
  SupplierOption,
  handleSearchSupplier,
  handleSearchUser,
  classBig,
  UserOptions,
  SpuOptions,
  handleSearchSpu,
}: {
  SupplierOption: IOptionItem[]
  handleSearchSupplier: (newVal: string) => void
  handleSearchUser: (newVal: string) => void
  handleSearchSpu: (newVal: string) => void
  classBig: IOptionItem[]
  UserOptions: IOptionItem[]
  SpuOptions: IOptionItem[]
}): IItemArray[] => {
  return [
    {
      label: '采购订单号',
      name: 'purchaseOrderCodes',
      placeholder: '多个单号间需要换行或使用","分隔',
      type: 'Select',
      config: {
        mode: 'tags',
        maxTagCount: 'responsive',
        tokenSeparators: [',', '，', '\n', '\r', '\r\n'],
        notFoundContent: null,
      },
    },
    {
      label: '正式订单时间',
      name: 'officialOrderTimeStart',
      type: 'FixedRangePicker',
      config: {
        showTime: false,
        customFormat: 'YYYY-MM-DD',
      },
      rules: [{ required: true, message: '请选择正式订单时间' }],
    },
    {
      name: 'supplierIdList',
      label: '供应商',
      type: 'Select',
      option: SupplierOption,
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        mode: 'multiple',
        maxTagCount: 'responsive',
        filterOption: false,
        onSearch: handleSearchSupplier,
        notFoundContent: <Empty description="请输入内容搜索" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
      },
    },
    {
      label: '大类',
      name: 'firstClassConfigIdList',
      type: 'Select',
      option: classBig,
      config: {
        showSearch: true,
        optionFilterProp: 'label',
        mode: 'multiple',
        maxTagCount: 'responsive',
      },
    },
    {
      label: '供应链跟单',
      name: 'trackOrderPersonWorkId',
      type: 'Select',
      option: UserOptions,
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: false,
        onSearch: handleSearchUser,
        notFoundContent: <Empty description="请输入内容搜索" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
        fieldNames: { label: 'label', value: 'workId' },
      },
    },
    {
      label: 'SPU商品',
      name: 'spuCode',
      type: 'Select',
      placeholder: '款式编码/款式名称/设计编码/厂家款号',
      option: SpuOptions,
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: false,
        onSearch: handleSearchSpu,
        notFoundContent: <Empty description="请输入内容搜索" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
      },
    },
  ]
}

export const useEditConfig = ({
  DICTDATA,
  form,
  impactDeliveryTime,
}: {
  DICTDATA: IDICTDATAType
  form: FormInstance
  impactDeliveryTime: boolean
}): IItemArray[] => {
  return [
    {
      name: 'responsibleParty',
      label: '责任方',
      type: 'Select',
      option: DICTDATA.resp_person,
      placeholder: '请选择责任方',
      config: {
        suffixIcon: null,
        showSearch: true,
        optionFilterProp: 'label',
        filterOption: true,
      },
      rules: [
        {
          required: true,
          message: '请选择责任方',
        },
      ],
    },
    {
      name: 'expReason',
      label: '异常原因',
      type: 'TextArea',
      placeholder: '请输入异常原因',
      config: {
        rows: 2,
        showCount: true,
        maxLength: 200,
      },
      rules: [
        {
          validator: () => validateAtLeastOneField(form, ['expReason', 'fileUrl'], '请输入异常原因或上传图片'),
        },
      ],
    },
    {
      name: 'fileUrl',
      label: '上传图片',
      config: { max: 1, showPaste: true },
      type: 'Upload',
      rules: [
        {
          validator: () => validateAtLeastOneField(form, ['expReason', 'fileUrl'], '请输入异常原因或上传图片'),
        },
      ],
    },
    {
      name: 'urgentRemedialMeasures',
      label: '紧急补救措施',
      type: 'TextArea',
      placeholder: '请输入紧急补救措施',
      config: {
        rows: 2,
        showCount: true,
        maxLength: 200,
      },
      rules: [
        {
          required: true,
          message: '请输入紧急补救措施',
        },
      ],
    },
    {
      name: 'longSolution',
      label: '长期解决方案',
      type: 'TextArea',
      placeholder: '请输入长期解决方案',
      config: {
        rows: 2,
        showCount: true,
        maxLength: 200,
      },
      rules: [
        {
          required: true,
          message: '请输入长期解决方案',
        },
      ],
    },
    {
      name: 'impactDeliveryTime',
      label: '是否影响交期',
      type: 'Radio',
      option: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    {
      name: 'updateDeliveryTime',
      label: '修正后供应链交期',
      type: 'FixedDatePicker',
      placeholder: '请选择修正后供应链交期',
      config: {
        customFormat: 'YYYY-MM-DD',
      },
      rules: [
        {
          required: true,
          message: '请选择修正后供应链交期',
        },
      ],
      hidden: !impactDeliveryTime,
    },
  ]
}
